-- 按钮类
Button = {
    text = nil, -- 按钮文本
    x = 0, -- X坐标
    y = 0, -- Y坐标
    height = 0, -- 高度
    width = 0, -- 宽度
    isDown = false, -- 按下状态
    isHover = false, -- 鼠标悬浮状态
    upColor = {0, 0, 0}, -- 按钮未被按下时颜色
    hoverColor = {0, 0, 0}, -- 鼠标悬停时颜色
    downColor = {0, 0, 0}, -- 按下时颜色
    downTextColor = {0, 0, 0}, -- 按下时文本的颜色
    upTextColor = {0, 0, 0} -- 鼠标悬停时文本颜色
}
-- 创建按钮
function Button:new(o, height, width, text, x, y)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    o.height = height or 0
    o.width = width or 0
    o.x = x or 0
    o.y = y or 0
    o.text = text or nil
    o.upColor = {64, 116, 56}
    o.hoverColor = {160, 191, 124}
    o.downColor = {101, 147, 74}
    o.downTextColor = {255, 255, 255}
    o.upTextColor = {0, 0, 0}
    return o
end
-- 绘制按钮
function Button:draw()
    if self.isHover then
        love.graphics.setColor(love.math.colorFromBytes(self.hoverColor))
    elseif self.isDown then
        love.graphics.setColor(love.math.colorFromBytes(self.downColor))
    else
        love.graphics.setColor(love.math.colorFromBytes(self.upColor))
    end
    love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)

    if self.isDown then
        love.graphics.setColor(love.math.colorFromBytes(self.downTextColor))
    else
        love.graphics.setColor(love.math.colorFromBytes(self.upTextColor))
    end
    love.graphics.setFont(Font)
    love.graphics.print(self.text, self.x + 5, self.y + 5)
end
-- 判断鼠标是否悬浮在按钮上
function Button:mouseHover(mouse)
    if (mouse.x > self.x and mouse.x < self.x + self.width and mouse.y > self.y and mouse.y < self.y + self.height) then
        self.isHover = true
        return true
    else
        self.isHover = false
        return false
    end
end
-- 判断x,y是否在按钮内
function Button:includeXY(x, y)
    if (x > self.x and x < self.x + self.width and y > self.y and y < self.y + self.height) then
        return true
    else
        return false
    end
end
-- 按下按钮
function Button:Down(x, y, button)
    if self:includeXY(x, y) and button == 1 then
        if self.isDown then
            self.isDown = false
            return false
        else
            self.isDown = true
            return true
        end
    end
end
